<?php
class AwardsController extends AppController {

	var $name = 'Awards';
	
	public $uses = array('Award', 'Member');
	
	public $paginate = array(
		'limit' => 100,
		'order' => array(
			'Award.date' => 'DESC'
		)
	);

	function index() {
		$this->Award->recursive = 0;
		$conditions = array(
			'OR' => array(
				'Award.member_id' => $this->Auth->user('member_id'),
				'Award.recommended_by_id' => $this->Auth->user('member_id'),
				'Award.approved_by_id' => $this->Auth->user('member_id')
			)
		);
        if ($this->Acl->check(array('model' => 'User', 'foreign_key' => $this->Auth->user('id')), 'Awards/view_others')) {
            $conditions['OR'] = array_merge($conditions['OR'], array(
            	'Award.location_id' => $this->Member->field('location_id', array('id' => $this->Auth->user('member_id')))
            ));
        }
        $this->paginate = array_merge($this->paginate, array('conditions' => $conditions));
		$this->set('awards', $this->paginate('Award'));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid award', true));
			$this->redirect(array('action' => 'index'));
		}
		
		$award = $this->Award->read(null, $id);
		if (
			$award['Award']['member_id'] != $this->Auth->user('member_id') &&
			$award['Award']['recommended_by_id'] != $this->Auth->user('member_id') &&
			$award['Award']['approved_by_id'] != $this->Auth->user('member_id') &&
			(
				$award['Award']['location_id'] != $this->Member->field('location_id', array('id' => $this->Auth->user('member_id'))) ||
				$this->Acl->check(array('model' => 'User', 'foreign_key' => $this->Auth->user('id')), 'Awards/view_others') == false
			)
		) {
			$this->Session->setFlash($this->Auth->authError, 'default', array(), 'auth');
			$this->redirect($this->Auth->loginRedirect);
		}
		
		$this->set('award', $award);
	}

	function add() {
		if (!empty($this->data)) {
			$this->Award->create();
			if ($this->Award->save($this->data)) {
				$this->set('message', __('The award has been saved', true));
				$this->data = array();
				$this->data['Award']['member_location_id'] = $this->Member->field('location_id', array('id' => $this->Auth->user('member_id')));
				$this->data['Award']['location_id'] = Configure::read('Pimp.location_id');
				$this->data['Award']['location_performed_id'] = Configure::read('Pimp.location_id');
			} else {
				$this->set('message', __('The award could not be saved. Please, try again.', true));
			}
		} else {
			$this->data['Award']['member_location_id'] = $this->Member->field('location_id', array('id' => $this->Auth->user('member_id')));
			$this->data['Award']['location_id'] = Configure::read('Pimp.location_id');
			$this->data['Award']['location_performed_id'] = Configure::read('Pimp.location_id');
		}
		$this->data['Award']['member_id'] = $this->Auth->user('member_id');
		$this->data['Award']['status'] = 'REQUESTED';
		$categories = $this->Award->Category->find('list');
		$locations = $this->Award->Location->find('list', array('order' => 'Location.code'));
		$locationPerformeds = $this->Award->PerformedLocation->find('list', array('order' => 'PerformedLocation.code'));
		$recommendedBies = $this->Award->RecommendedBy->find('list');
		$approvedBies = $this->Award->ApprovedBy->find('list');
        $this->set('busyIndicatorClass', $this->RequestHandler->isAjax() ? 'visible' : 'hidden');
		$this->set(compact('members', 'categories', 'recommendedBies', 'approvedBies', 'locationPerformeds', 'locations', 'defaultLocation'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid award', true));
			$this->redirect(array('action' => 'index'));
		}
		
		$award = $this->Award->read(null, $id);
		if (
			!(
				$award['Award']['status'] != 'REQUESTED' ||
				(
					$award['Award']['member_id'] != $this->Auth->user('member_id') &&
					(
						$award['Award']['location_id'] != $this->Member->field('location_id', array('id' => $this->Auth->user('member_id'))) ||
						$this->Acl->check(array('model' => 'User', 'foreign_key' => $this->Auth->user('id')), 'Awards/edit_others') == false
					)
				)
			) ||
			!(
				$award['Award']['status'] != 'RECOMMENDED' ||
				(
					$award['Award']['recommended_by_id'] != $this->Auth->user('member_id') &&
					(
						$award['Award']['location_id'] != $this->Member->field('location_id', array('id' => $this->Auth->user('member_id'))) ||
						$this->Acl->check(array('model' => 'User', 'foreign_key' => $this->Auth->user('id')), 'Awards/edit_others') == false
					)
				)
			) ||
			!(
				$award['Award']['status'] != 'AWARDED' ||
				(
					$award['Award']['awarded_by_id'] != $this->Auth->user('member_id') &&
					(
						$award['Award']['location_id'] != $this->Member->field('location_id', array('id' => $this->Auth->user('member_id'))) ||
						$this->Acl->check(array('model' => 'User', 'foreign_key' => $this->Auth->user('id')), 'Awards/edit_others') == false
					)
				)
			)
		) {
			$this->Session->setFlash('You do not have permission to edit that award.');
			$this->redirect(array('action' => 'index'));
		}
		
		if (!empty($this->data)) {
			if ($this->Award->save($this->data)) {
				$this->Session->setFlash(__('The award has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The award could not be saved. Please, try again.', true));
			}
		}
		
		if (empty($this->data)) {
			$this->data = $award;
		}
		
		$members = $this->Award->Member->find('list');
		$categories = $this->Award->Category->find('list');
		$performedLocations = $this->Award->PerformedLocation->find('list', array('order' => 'PerformedLocation.code'));
		$locations = $this->Award->Location->find('list', array('order' => 'Location.code'));
		$recommendedBies = $this->Award->RecommendedBy->find('list');
		$approvedBies = $this->Award->ApprovedBy->find('list');
		$this->set(compact('members', 'categories', 'recommendedBies', 'approvedBies', 'locations', 'performedLocations'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for award', true));
			$this->redirect(array('action'=>'index'));
		}
		
		$award = $this->Award->read(null, $id);
		if (
			(
				$award['Award']['status'] != 'REQUESTED' ||
				(
					$award['Award']['member_id'] != $this->Auth->user('member_id') &&
					(
						$award['Award']['location_id'] != $this->Member->field('location_id', array('id' => $this->Auth->user('member_id'))) ||
						$this->Acl->check(array('model' => 'User', 'foreign_key' => $this->Auth->user('id')), 'Awards/delete_others') == false
					)
				)
			) &&
			(
				$award['Award']['status'] != 'RECOMMENDED' ||
				(
					$award['Award']['recommended_by_id'] != $this->Auth->user('member_id') &&
					(
						$award['Award']['location_id'] != $this->Member->field('location_id', array('id' => $this->Auth->user('member_id'))) ||
						$this->Acl->check(array('model' => 'User', 'foreign_key' => $this->Auth->user('id')), 'Awards/delete_others') == false
					)
				)
			) &&
			(
				$award['Award']['status'] != 'AWARDED' ||
				(
					$award['Award']['awarded_by_id'] != $this->Auth->user('member_id') &&
					(
						$award['Award']['location_id'] != $this->Member->field('location_id', array('id' => $this->Auth->user('member_id'))) ||
						$this->Acl->check(array('model' => 'User', 'foreign_key' => $this->Auth->user('id')), 'Awards/delete_others') == false
					)
				)
			)
		) {
			$this->Session->setFlash($this->Auth->authError, 'default', array(), 'auth');
			$this->redirect($this->Auth->loginRedirect);
		}
		
		if ($this->Award->delete($id)) {
			$this->Session->setFlash(__('Award deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Award was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	public function recommend() {
        
	}
	
	public function approve() {
	    
	}
}
?>